local qirang = fk.CreateSkill {
  name = "mini__qirang",
}

Fk:loadTranslationTable{
  ["mini__qirang"] = "祈禳",
  [":mini__qirang"] = "当装备牌置入你的装备区后，你可以从牌堆随机获得一张锦囊牌，你本回合使用以此法获得的普通锦囊牌可以额外指定一个目标。",

  ["@@mini__qirang-turn-inhand"] = "祈禳",
  ["#mini__qirang-choose"] = "祈禳：你可以为%arg增加一个目标",

  ["$mini__qirang1"] = "谨书尺素，上告穹苍。",
  ["$mini__qirang2"] = "祈禳上苍，以渡下民。",
}

qirang:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qirang.name) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Equip then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = room:getCardsFromPileByRule(".|.|.|.|.|trick")
    if #cids > 0 then
      local cid = cids[1]
      room:addTableMark(player, "mini__qirang-turn", cid)
      room:obtainCard(player, cid, false, fk.ReasonPrey, player, qirang.name, "@@mini__qirang-turn-inhand")
    end
  end,
})

qirang:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.card:isCommonTrick() and
      table.contains(player:getTableMark("mini__qirang-turn"), data.card.id) and
      #data:getExtraTargets() > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = data:getExtraTargets(),
      min_num = 1,
      max_num = 1,
      prompt = "#mini__qirang-choose:::" .. data.card:toLogString(),
      skill_name = qirang.name,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    data:addTarget(to)
    room:sendLog{
      type = "#AddTargetsBySkill",
      from = player.id,
      to = { to.id },
      arg = qirang.name,
      arg2 = data.card:toLogString(),
    }
  end,
})

return qirang
